<?php
defined('IN_IA') or exit('Access Denied');

global $_W, $_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
//视频列表页
if ($op == 'display') {

    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    $condition = " uniacid = '{$_W['uniacid']}'";
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('aide_activity_photo') . " WHERE $condition");
    $photo=pdo_fetchall("SELECT * FROM " . tablename('aide_activity_photo') . " WHERE uniacid='{$_W['uniacid']}' ORDER BY  id DESC");
    //图片库
    $url='http://video.aidemed.com/api/data/raceList';
    $temp=json_decode(curl_get($url),true);
    foreach ($temp as $value){
        $activity[$value['raceid']]=$value;
    }
    //记录总数
    $pager = pagination($total, $pindex, $psize);
    include aide_template ('photo');
    exit;
}

if ($op == 'post') {
    $photoid = intval($_GPC['photoid']);
    $photo=pdo_fetchall("SELECT * FROM " . tablename('aide_activity_photo') . " WHERE id=$photoid ORDER BY  id ASC LIMIT 1");
    $photo=$photo[0];
    $arr=pdo_fetchall("SELECT id,title FROM " . tablename('aide_activity') . " WHERE uniacid='{$_W['uniacid']}' ORDER BY  id ASC");
    foreach ($arr as $value){
        $value['link']=app_url('home/index/detail').'&activityid='.$value['id'];
        $temp[$value['id']]=$value;
    }
    //图片库
    $url='http://video.aidemed.com/api/data/raceList';
    $activity=json_decode(curl_get($url),true);
    $time=date('Y-m-d H:i:s');
    if (checksubmit('submit')) {
        $form = $_GPC['photo'];
        $data['uniacid'] = $_W['uniacid'];
        $data['raceid'] = $form['raceid'];
        $data['bgimg'] = $form['bgimg'];
        $data['link'] = $form['link'];
        $data['atlas']=serialize($_GPC['img']);
        $data['share_title'] = $form['share_title'];
        $data['share_desc']= $form['share_desc'];
        $data['share_link'] = $form['share_link'];
        $data['share_img']= $form['share_img'];
        if (!empty($photoid)) {
            pdo_update ('aide_activity_photo', $data, array ('id' => $photoid));
        } else {
            pdo_insert ('aide_activity_photo', $data);
            $activityid = pdo_insertid();
        }
        message ('更新成功！', web_url('photo/photo/display'), 'success');
    }
    include aide_template ('photo');
    exit;
}

if ($op == 'delete') {
    $photoid = intval($_GPC['photoid']);
    pdo_delete ('aide_activity_photo', array ('id' => $photoid));
    die(json_encode(array("errno" => 0,'message'=>'删除成功')));
    exit;
}
function curl_get($url){
    $header = array(
        'Accept: application/json',
    );
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 超时设置,以秒为单位
    curl_setopt($curl, CURLOPT_TIMEOUT, 1);
    // 超时设置，以毫秒为单位
//     curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
    // 设置请求头
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    //执行命令
    $data = curl_exec($curl);
    // 显示错误信息
    if (curl_error($curl)) {
        print "Error: " . curl_error($curl);
    } else {
        // 打印返回的内容
        curl_close($curl);
        return $data;

    }
}
